package indexing;

import java.io.File;
import java.io.IOException;
import java.util.regex.Pattern;

import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class IndexProducts {

	private StandardAnalyzer analyzer_3;
	private FSDirectory dir_3;
	private IndexWriterConfig config_3;
	private IndexWriter w_3;
	private Document doc;
	//splitter patterns
	private static Pattern pLineSplit=Pattern.compile("\n");
	private static Pattern pfieldSplit=Pattern.compile("\t");
	//Attributes used in the method "generateProductIndex()"
	private String[] linesArray;
	private String[] fieldsArray;
	private int lineCont;
	private String id;
	private String name;
	private String type;
	private String brand;
	/**
	 * Method called to initialize the attributes of the class
	 * @param String indexDir Main directory where the folder index_p will be stored- 
	 */
	public void init(String indexDir)throws Exception{
		/**
		 * Example of use of PerFieldAnalyzer:
		 * 	http://lucene.472066.n3.nabble.com/PerFieldAnalyzerWrapper-use-Analyzer-s-not-being-used-as-expected-td541413.html
		 * new way
		 * 	http://lucene.apache.org/core/old_versioned_docs/versions/3_5_0/api/core/org/apache/lucene/analysis/PerFieldAnalyzerWrapper.html
		 */
		this.analyzer_3 = new StandardAnalyzer(Version.LUCENE_35);
		if(System.getProperty("os.name").toLowerCase().indexOf("win")>=0)
			this.dir_3 = FSDirectory.open(new File(indexDir+"\\index_p"));
		else
			this.dir_3 = FSDirectory.open(new File(indexDir+"/index_p"));
		this.config_3 = new IndexWriterConfig(Version.LUCENE_35,analyzer_3);
		this.w_3=new IndexWriter(this.dir_3, this.config_3);
	}
	public void closeIndexWriter()throws IOException{
		w_3.close();
	}
	/**
	 * Create a new document in the Index_p structure and add the specified fields
	 * @param String f_id, String f_name, String f_type, String f_brand
	 */
	public void addDocIndex_1(String f_id,String f_name,String f_type,String f_brand) throws IOException {
	    this.doc= new Document();
	    this.doc.add((Fieldable) new Field("p_id", f_id, Field.Store.YES, Field.Index.NO));
	    this.doc.add((Fieldable) new Field("p_name", f_name, Field.Store.YES, Field.Index.ANALYZED));
	    this.doc.add((Fieldable) new Field("p_type", f_type, Field.Store.YES, Field.Index.ANALYZED));
	    this.doc.add((Fieldable) new Field("p_brand", f_brand, Field.Store.YES, Field.Index.ANALYZED));
	    //the field description was not take into account because it was not clear the structure of this field, in order to parse it.
	    //this.doc.add((Fieldable) new Field("p_desc", f_desc, Field.Store.YES, Field.Index.NO));
	    this.w_3.addDocument(this.doc);
	 }
	/**
	 * This method receives a information block and process it identifying the products and indexing them.
	 * @param String infoBlock Information block to be processed 
	 * @throws IOException 
	 */
	public void generateProductIndex(String infoBlock) throws IOException{
		this.linesArray=pLineSplit.split(infoBlock);
		for(this.lineCont=0;this.lineCont<this.linesArray.length;this.lineCont++){
			//In the file the string "BREAK-REVIEWED" represents the beginning of a new product instance.
			if(this.linesArray[this.lineCont].contains("BREAK-REVIEWED")||
					this.linesArray[this.lineCont].contains("->")){
				/* A new product review start
				 * If the are information to be store index the info, else continue with the next line.
				 */
				if(this.id!=null){
					if(!this.id.isEmpty())
						addDocIndex_1(this.id, this.name, this.type, this.brand);
				}
				else
					continue;
			}else{
				this.fieldsArray=pfieldSplit.split(this.linesArray[this.lineCont]);
				if(this.fieldsArray.length>1){
					this.id=this.fieldsArray[0];
					if(this.fieldsArray[1]!=null)
						this.name=this.fieldsArray[1];
					else
						this.name="UNKOWN";
					if(this.fieldsArray[2]!=null)
						this.type=this.fieldsArray[2];
					else
						this.type="UNKOWN";
					if(this.type.isEmpty())
						this.type="UNKOWN";
					this.brand=this.fieldsArray[3];
					if(this.brand.isEmpty())
						this.brand="UNKOWN";
				}
			}			
		}
	}
}
